classdef Maze<handle
    properties
        mazeHeight;
        mazeWidth;
        mazeData;
        mazeMap = [0 0 0;1 1 1];
    end
    methods
        % 构造函数
        function obj = Maze(mazeWidth, mazeHeight)
            % 检查参数个数，如果少于1个，则使用默认值
            if nargin < 1
                mazeWidth = 50; % 设置 mazeWidth 的默认值为 50
                mazeHeight = 50;% 设置 mazeHeight 的默认值为 50
            end
            obj.mazeWidth = mazeWidth;
            obj.mazeHeight = mazeHeight;
            obj.mazeData = zeros(mazeWidth,mazeHeight);
            obj.mazeMap = [0 0 0;1 1 1;0 1 0;1 0 0];% 颜色映射[墙壁方块 可通行方块 起点 终点]
            figure('name','maze','position',[460 100 840 800]);% 设置图窗大小
        end
        % 生成迷宫
        function [mazeData,obj] = generate(obj)
            obj.mazeData = ones(obj.mazeWidth,obj.mazeHeight);
            index= [];
            % 直线障碍
            index = [index;lineObstaclesIndex([16,19],[19,8])];
            index = [index;lineObstaclesIndex([24,19],[19,8])];
            index = [index;lineObstaclesIndex([24,19],[21,35])];
            index = [index;lineObstaclesIndex([16,19],[21,35])];
            
            index = [index;lineObstaclesIndex([26,24],[31,27])];
            index = [index;lineObstaclesIndex([26,29],[31,27])];
            index = [index;lineObstaclesIndex([26,29],[26,24])];
            
            index = [index;lineObstaclesIndex([3,4],[19,3])];
            index = [index;lineObstaclesIndex([19,3],[8,19])];
            index = [index;lineObstaclesIndex([3,4],[8,19])];
            
            index = [index;lineObstaclesIndex([3,16],[8,25])];
            index = [index;lineObstaclesIndex([13,15],[8,25])];
            index = [index;lineObstaclesIndex([13,15],[16,35])];
            index = [index;lineObstaclesIndex([8,41],[16,35])];
            index = [index;lineObstaclesIndex([8,41],[3,16])];
 
            index = [index;lineObstaclesIndex([20,1],[32,1])];
            index = [index;lineObstaclesIndex([23,10],[32,1])];
            index = [index;lineObstaclesIndex([23,10],[20,1])];
             
            index = [index;lineObstaclesIndex([16,50],[21,39])];
            index = [index;lineObstaclesIndex([31,32],[21,39])];
            index = [index;lineObstaclesIndex([31,32],[37,41])];
            index = [index;lineObstaclesIndex([40,41],[37,41])];
            index = [index;lineObstaclesIndex([40,41],[40,50])];
            index = [index;lineObstaclesIndex([16,50],[40,50])];

            index = [index;lineObstaclesIndex([29,16],[34,25])];
            index = [index;lineObstaclesIndex([44,17],[34,25])];
            index = [index;lineObstaclesIndex([44,17],[38,4])];
            index = [index;lineObstaclesIndex([29,16],[38,4])];
            
            index = [index;lineObstaclesIndex([43,1],[44,9])];
            index = [index;lineObstaclesIndex([50,20],[44,9])];
            
            index = [index;lineObstaclesIndex([38,27],[41,27])];
            index = [index;lineObstaclesIndex([41,34],[41,27])];
            index = [index;lineObstaclesIndex([41,34],[38,34])];
            index = [index;lineObstaclesIndex([34,31],[38,34])];
            index = [index;lineObstaclesIndex([34,31],[38,27])];
            
            index = [index;lineObstaclesIndex([42,39],[46,21])];
            index = [index;lineObstaclesIndex([47,42],[46,21])];
            index = [index;lineObstaclesIndex([47,42],[42,47])];
            index = [index;lineObstaclesIndex([42,39],[42,47])];
            
            index = [index;lineObstaclesIndex([44,50],[46,47])];
            index = [index;lineObstaclesIndex([48,50],[46,47])];
            index = [index;lineObstaclesIndex([48,50],[44,50])];

            index = [index;lineObstaclesIndex([50,28],[49,34])];
            index = [index;lineObstaclesIndex([50,47],[49,34])];
            index = [index;lineObstaclesIndex([50,47],[50,28])];
            
            for i = 1:length(index(:,1))
                % 迷宫矩阵的横轴为列和纵轴为行
                obj.mazeData(index(i,2),index(i,1)) = 0;
            end
            obj.mazeData(1,1) = 2;
            obj.mazeData(end,end) = 3;
            mazeData = obj.mazeData;
            function index = lineObstaclesIndex(startPoint,endPoint)
                index = [];
                if startPoint == endPoint
                    index = [startPoint(1),startPoint(2)];
                    return;
                end
%                 index=[startPoint(1),startPoint(2)];
%                 index=[index;[endPoint(1),endPoint(2)]];
                dx = startPoint(1) - endPoint(1);
                dy = startPoint(2) - endPoint(2);
                sequenceX = min(startPoint(1),endPoint(1)):max(startPoint(1),endPoint(1));
                sequenceY = min(startPoint(2),endPoint(2)):max(startPoint(2),endPoint(2));
                if dx == 0
                    for y = sequenceY
                        index = [index;[startPoint(1),y]];                       
                    end
                elseif dy == 0
                    for x = sequenceX
                        index = [index;[x,startPoint(2)]];                       
                    end
                elseif abs(dy) >= abs(dx)
                    k = dy/dx;b = endPoint(2) - k*endPoint(1);
                    for y = sequenceY
                       index = [index;[round((y-b)/k),y]];
                    end
                elseif abs(dy) < abs(dx)
                    k = dy/dx;b = endPoint(2) - k*endPoint(1);
                    for x = sequenceX
                       index = [index;[x,round(k*x+b)]];
                    end
                end
            end
        end
        % 绘制迷宫
        function draw(obj)
            imagesc(obj.mazeData)% 显示迷宫
            colormap(obj.mazeMap)
            % 铺满屏幕并关闭刻度显示
            ax = gca;
            ax.Position=[0 0 1 0.95];
            ax.XTick = [];
            ax.YTick = [];
            axis off
        end
    end
end